#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QRegExpValidator>
#include <QFileDialog>
#include <QDebug>

#include "functions.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    downloader(new Downloader)
{
    ui->setupUi(this);

    // no need of ^ or $
    QRegExp re("http://.+/.+");
    auto validator = new QRegExpValidator(re);
    ui->urlLineEdit->setValidator(validator);
    QString defaultUrl = "http://";
    ui->urlLineEdit->setText(defaultUrl);
    ui->urlLineEdit->setSelection(0, defaultUrl.length());

    QString downloadsPath = QDir::homePath() + QDir::separator() + "Downloads";
    ui->savePathLineEdit->setText(downloadsPath);

    connect(downloader, SIGNAL(finished()), this, SLOT(downloadFinished()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_urlLineEdit_textChanged(const QString)
{
    bool validUrl = ui->urlLineEdit->hasAcceptableInput();
    ui->startButton->setEnabled(validUrl);

    if(validUrl)
    {
        QUrl url(ui->urlLineEdit->text());
        auto fileName = getFileName(url.path());
        ui->fileNameLineEdit->setText(fileName);
    }
}

void MainWindow::on_browseButton_clicked()
{
    QString path = QFileDialog::getExistingDirectory(0, "Choose download directory", saveDir.path());
    ui->savePathLineEdit->setText(path);

    // update save Directory
    saveDir.setPath(path);
}

void MainWindow::on_savePathLineEdit_textChanged(const QString &path)
{
    // check if given path is valid
    // if invalid then show in red color
    if(QDir(path).exists()) {
        ui->savePathLineEdit->setStyleSheet("");
        // update save Directory
        saveDir.setPath(path);
    } else {
        ui->savePathLineEdit->setStyleSheet("color: red");
    }
}

void MainWindow::on_startButton_clicked()
{
    if(ui->fileNameLineEdit->text().isEmpty())
    {
        qDebug() << "file name cannot be empty";
        return;
    }

    ui->textEdit->append("started download...\n");

    ui->progressBar->reset();
    ui->urlLineEdit->setDisabled(true);
    ui->num_threads->setDisabled(true);
    ui->startButton->setDisabled(true);
    ui->browseButton->setDisabled(true);
    ui->pauseButton->setDisabled(true);
    ui->resumeButton->setDisabled(false);
    ui->savePathLineEdit->setDisabled(true);

    // no of thread
    auto nt = ui->num_threads->value();

    downloader->startDownload(ui->urlLineEdit->text(), saveDir, ui->fileNameLineEdit->text(), nt);

    connect(downloader, SIGNAL(progressChanged(int)), ui->progressBar, SLOT(setValue(int)));
    connect(downloader, SIGNAL(errorOccured(QString)), this, SLOT(onDownloadError(QString)));
}

void MainWindow::downloadFinished()
{
    ui->textEdit->append("Download Finished!!!");
    qDebug("Downloader deleted");
    downloader->deleteLater();
    //delete downloader;
}

void MainWindow::onDownloadError(QString error)
{
    ui->textEdit->append("Error: " + error);
}

void MainWindow::on_fileNameLineEdit_textChanged(const QString &fileName)
{
    qDebug() << fileName;
}
